package cn.hbu.zlfw.aspect.service;

import java.lang.reflect.Method;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hbu.zlfw.annotation.OperatorLog;
import cn.hbu.zlfw.aspect.dao.OperationLogDao;
import cn.hbu.zlfw.aspect.entity.OperatorLogVo;
import cn.hbu.zlfw.common.util.PropertiesUtil;
import cn.hbu.zlfw.common.util.SessionUtil;
import cn.hbu.zlfw.common.util.Utility;

/**
 * 
 *@ClassName:OperationLogService.java
 *@Description:  操作日志记录,记录用户的操作
 *@Author:lxt<839376636@qq.com>
 *@Date:2017年10月7日下午5:01:04
 *@Version:1.1.0
 */
@Aspect
@Service("operationLogService")
@Transactional
public class OperationLogService {

	/**
	 * 日志
	 */
	public final Logger logger = LoggerFactory.getLogger(OperationLogService.class);
	
	private static final String PATH = "/operator_log.properties";
	
	/**
	 * 注入OperationLogDao接口
	 */
	@Autowired
	private OperationLogDao operationLogDao;
	/**
	 * 注入HttpServletRequest
	 */
//	@Autowired  
//	private HttpServletRequest request;
	
	
	/**
	 * 声明切入点，拦截使用注解：@OperatorLog的方法
	 */
    @Pointcut("@annotation(cn.hbu.zlfw.annotation.OperatorLog)")
    public void logPonitCut() {
    }
    /**
     * 后置通知，记录用户成功的操作
     * @throws Exception 
     */
    @After("logPonitCut()")
    public void afterLogPonitCut(JoinPoint joinPoint) throws Exception {
//    	logger.debug("记录用户日志操作，开始执行...");
//    	//获取用户信息
//    	Object userObject = SessionUtil.getLoginUser(request);
//    	//判断用户是否登录
//    	if(userObject==null) {
//    		logger.debug("用户未登录，执行结束！");
//    		return;
//    	}
//    	OperatorLogVo operatorLogVo = new OperatorLogVo();
//    	/**
//    	 * 通过反射获取用户的用户名和用户类型
//    	 */
//    	String userNamePro = PropertiesUtil.getInstance().loadProperties(PATH, "userName");
//		String userTypePro = PropertiesUtil.getInstance().loadProperties(PATH, "userType");
//    	Object userNameObject = Utility.getPropertyCollectByReflect(userObject, userNamePro);
//    	String userName = userNameObject==null?null:userNameObject.toString();
//    	Object userTypeObject = Utility.getPropertyCollectByReflect(userObject, userTypePro);
//    	operatorLogVo.setUserName(userName);
//    	operatorLogVo.setUserType(userTypeObject==null?-1:Integer.valueOf(userTypeObject.toString()));
//    	//获取方法上的所有注解=》获取操作类型和操作信息
//    	OperatorLog operatorLog = this.getOperatorLog(joinPoint);
//    	if(operatorLog==null) {
//    		logger.debug("记录用户日志操作停止，系统异常！");
//    		return;
//    	}
//    	operatorLogVo.setDescription(operatorLog.description());
//		operatorLogVo.setOperatorType(operatorLog.type().getTypeName());
//		operatorLogVo.setCreateTime(new Date());	
//		operatorLogVo.setDelState(0);
//		this.operationLogDao.save(operatorLogVo);
//		logger.debug("用户《{}》执行了《{}》操作。",userName,operatorLog.description());
//    	logger.debug("记录用户日志操作，执行结束！");
    }	
	
    /**
     * 
     * @Title: getOperatorLog 
     * @Description: 内部调用，获取操作日志注解对象实例
     * @param joinPoint
     * @return
     * @throws Exception String
     */
    private OperatorLog getOperatorLog(JoinPoint joinPoint)  
            throws Exception {  
       String targetName = joinPoint.getTarget().getClass().getName();  
       String methodName = joinPoint.getSignature().getName();  
       Object[] arguments = joinPoint.getArgs();  
       Class targetClass = Class.forName(targetName);  
       Method[] methods = targetClass.getMethods();  
        for(Method method : methods) {  
            if(method.getName().equals(methodName)) {  
               Class[] clazzs = method.getParameterTypes();  
                if(clazzs.length == arguments.length) {  
                   return method.getAnnotation(OperatorLog.class);  
               }  
           }  
       }  
       return null;  
   }  
}
